//
//  MakeSchemeViewController.m
//  AnXin100
//
//  Created by Sakura on 2018/11/10.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "MakeSchemeViewController.h"
#import "AXMyWdjsSchemeDetailsFirstCell.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "Header.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "UIViewController+HBD.h"
#import "AXMyWdjsSchemeDetailsEightCell.h"
#import "AXMyWdjsSchemeDetailsSecondCell.h"
#import "MakeSchemeThirdTableViewCell.h"
#import "MakeSchemeDiseaseTableViewCell.h"
#import "MakeSchemeShortManureTableViewCell.h"
#import "MakeSchemeExplainTableViewCell.h"
#import "SolutionTableViewCell.h"
@interface MakeSchemeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *mainTable;



@end

@implementation MakeSchemeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainTable];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    footerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake((SCREEN_WIDTH - 230) / 2, 10, 110, 30);
    saveBtn.backgroundColor = [UIColor colorWithHex:0xf6f6f6];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor colorWithHex:0x666666] forState:UIControlStateNormal];
    saveBtn.layer.cornerRadius = 15;
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [footerView addSubview:saveBtn];
    
    
    UIButton *issueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    issueBtn.frame = CGRectMake(CGRectGetMaxX(saveBtn.frame) + 10, 10, 110, 30);
    issueBtn.backgroundColor = COLOR_GREEN;
    [issueBtn setTitle:@"发布" forState:UIControlStateNormal];
    issueBtn.layer.cornerRadius = 15;
    issueBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [footerView addSubview:issueBtn];
    
    _mainTable.tableFooterView = footerView;
}

- (UITableView *)mainTable {
    if (!_mainTable) {
        _mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, Hn, SCREEN_WIDTH, SCREEN_HEIGHT - Hn) style:UITableViewStyleGrouped];
        _mainTable.delegate = self;
        _mainTable.dataSource = self;
        _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_mainTable registerClass:[AXMyWdjsSchemeDetailsFirstCell class] forCellReuseIdentifier:@"AXMyWdjsSchemeDetailsFirstCell"];
        [self.mainTable registerClass:[AXMyWdjsSchemeDetailsSecondCell class] forCellReuseIdentifier:@"AXMyWdjsSchemeDetailsSecondCell"];
        [_mainTable registerClass:[MakeSchemeThirdTableViewCell class] forCellReuseIdentifier:@"MakeSchemeThirdTableViewCell"];
        [_mainTable registerClass:[MakeSchemeDiseaseTableViewCell class] forCellReuseIdentifier:@"MakeSchemeDiseaseTableViewCell"];
        [_mainTable registerClass:[MakeSchemeShortManureTableViewCell class] forCellReuseIdentifier:@"MakeSchemeShortManureTableViewCell"];
        [_mainTable registerNib:[UINib nibWithNibName:@"MakeSchemeExplainTableViewCell" bundle:nil] forCellReuseIdentifier:@"MakeSchemeExplainTableViewCell"];
        [_mainTable registerNib:[UINib nibWithNibName:@"SolutionTableViewCell" bundle:nil] forCellReuseIdentifier:@"SolutionTableViewCell"];
    }
    return _mainTable;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSArray *array = @[@"病害及防治方法", @"病害防治施肥方案", @"虫害及防治方法",@"草害及防治方法", @"缺素症及施肥方案", @""];
    if (section == 3 || section == 4 || section == 5 || section == 6 || section == 7 || section == 6) {
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        UIView *corView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH - 20, 40)];
        corView.backgroundColor = [UIColor whiteColor];
        corView.layer.cornerRadius = 5;
        corView.layer.masksToBounds = YES;
        [headView addSubview:corView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 40)];
        titleLabel.text = array[section - 2];
        titleLabel.font = [UIFont systemFontOfSize:14];
        [corView addSubview:titleLabel];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        if (section == 4 || section == 7) {
            button.frame = CGRectMake(SCREEN_WIDTH - 60, 5, 30, 30);
            [button setImage:[UIImage imageNamed:@"addother"] forState:UIControlStateNormal];
        } else {
            button.frame = CGRectMake(SCREEN_WIDTH - 100, 10, 70, 20);
            [button setImage:[UIImage imageNamed:@"skip"] forState:UIControlStateNormal];
            [button setTitle:@"请选择" forState:UIControlStateNormal];
            [button setTitleColor:COLOR_Gray forState:UIControlStateNormal];
            button.titleLabel.font = FONT_TWELVE;
            [button setButtonLayout:CHButtonLayoutTitleLeft offset:2];
        }
        [corView addSubview:button];
        return headView;

    } else {
        return [UIView new];
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 3 || section == 4 || section == 5 || section == 6 || section == 7 || section == 6) {
        return 50;

    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 9;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0: {
            
            return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[AXMyWdjsSchemeDetailsFirstCell class] contentViewWidth:SCREEN_WIDTH];
            break;
        }
        case 1: {
            
            return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[AXMyWdjsSchemeDetailsSecondCell class] contentViewWidth:SCREEN_WIDTH];
            break;
        }
        case 2: {
            return 200;
        }
        
        case 3: {
            return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[MakeSchemeThirdTableViewCell class] contentViewWidth:SCREEN_WIDTH];
            break;
        }
        case 4: {
            
            return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[MakeSchemeDiseaseTableViewCell class] contentViewWidth:SCREEN_WIDTH];
            break;
        }
        case 5: {
            
            return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[MakeSchemeThirdTableViewCell class] contentViewWidth:SCREEN_WIDTH];
            break;
        }
        case 6: {
            
            return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[MakeSchemeThirdTableViewCell class] contentViewWidth:SCREEN_WIDTH];
            break;
        }
            
        case 7: {
            
            return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[MakeSchemeShortManureTableViewCell class] contentViewWidth:SCREEN_WIDTH];
            break;
        }
        case 8: {
            
            return 230;
            break;
        }
            
        default:
            break;
    }
    return [tableView cellHeightForIndexPath:indexPath model:nil keyPath:@"model" cellClass:[AXMyWdjsSchemeDetailsEightCell class] contentViewWidth:SCREEN_WIDTH];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        AXMyWdjsSchemeDetailsFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AXMyWdjsSchemeDetailsFirstCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        cell.bgView.sd_layout
        .leftSpaceToView(cell.contentView, 10)
        .topSpaceToView(cell.contentView, 10)
        .widthIs(SCREEN_WIDTH-20)
        .heightIs(190);
        return cell;
    }
    
    if (indexPath.section == 1) {
        AXMyWdjsSchemeDetailsSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AXMyWdjsSchemeDetailsSecondCell" forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    
    if (indexPath.section == 2) {
        SolutionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SolutionTableViewCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    
    if (indexPath.section == 3) {
        MakeSchemeThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MakeSchemeThirdTableViewCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    
    if (indexPath.section == 4) {
        MakeSchemeDiseaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MakeSchemeDiseaseTableViewCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    
    if (indexPath.section == 5) {
        MakeSchemeThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MakeSchemeThirdTableViewCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    
    if (indexPath.section == 6) {
        MakeSchemeThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MakeSchemeThirdTableViewCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    
    if (indexPath.section == 7) {
        MakeSchemeShortManureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MakeSchemeShortManureTableViewCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    
    if (indexPath.section == 8) {
        MakeSchemeExplainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MakeSchemeExplainTableViewCell"];
//        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return cell;
    }
    return nil;
}




@end
